package com.tensquare.gathering.controller;

import com.tensquare.gathering.pojo.Gathering;
import com.tensquare.gathering.service.GatheringService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
/**
 * @Author:zimmor
 * @Date: 2021/3/2 17:29
 * @Description
 * @version: 1.0.0
 */
@RestController
@CrossOrigin
@RequestMapping("/gathering")
public class GatheringController {
    @Autowired
    private GatheringService gatheringService;

    /**
     * 分页查询
     * @param gathering
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/search/{page}/{size}")
    public Result findSearch(@RequestBody Gathering gathering, @PathVariable int page, @PathVariable int size) {
        Page pageData = gatheringService.findSearch(gathering, page, size);
        return new Result(true, StatusCode.OK, "查询成功", new PageResult<Gathering>(pageData.getTotalElements(),pageData.getContent()));
    }

    /**
     * 条件查询
     * @param gathering
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/search")
    public Result findSearch(@RequestBody Gathering gathering) {
        return new Result(true, StatusCode.OK, "查询成功", gatheringService.findSearch(gathering));
    }

    /**
     * 查询所有
     * @return
     */
    @RequestMapping()
    public Result findAll() {
        return new Result(true, StatusCode.OK, "查询成功", gatheringService.findAll());
    }

    @RequestMapping(method = RequestMethod.GET, value = "/{id}")
    public Result findById(@PathVariable String id) {
        return new Result(true, StatusCode.OK, "查询成功", gatheringService.findById(id));
    }

    @RequestMapping(method = RequestMethod.POST)
    public Result add(@RequestBody Gathering gathering) {
        gatheringService.add(gathering);
        return new Result(true,StatusCode.OK, "新增成功");
    }

    @RequestMapping(method = RequestMethod.PUT, value = "/{id}")
    public Result update(@RequestBody Gathering gathering, @PathVariable String id) {
        gathering.setId(id);
        gatheringService.update(gathering);
        return new Result(true, StatusCode.OK, "修改成功");
    }
    @RequestMapping(method = RequestMethod.DELETE, value = "/{id}")
    public Result deleteById(@PathVariable String id) {
        gatheringService.deleteById(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }
}
